import { Module } from "vuex";
import {
  State,
  Mutations,
  Actions,
  Getters,
  UserData,
  UserResetPwdData,
} from "@/types/userTypes";
import { getAccessToken, stopRefreshTimer } from "@/modules/auth"; // 引入 getAccessToken
// import router from "@/router";

// 定义 store 的状态
const state: State = {
  userInfo: null,
  isLoggedIn: false,
  isRegistering: false, // 添加注册状态
  isForgotPassword: false, // 添加重置密码状态
  accessToken: null, // 添加 accessToken 字段
  formState: {
    // 添加 formState 字段
    username: "",
    email: "",
    password: "",
    confirmPassword: "",
  },
  resetPwdFormState: {
    username: "",
    email: "",
  },
};

// 定义 Mutations
const mutations: Mutations = {
  setUser(state, userData: UserData) {
    state.userInfo = userData;
    if (userData.name) {
      state.userInfo.username = userData.name; // 修改 username 字段
    }
    state.isLoggedIn = true;
  },
  setIsLoggedIn(state, status) {
    state.isLoggedIn = status;
  },
  setIsRegistering(state, status) {
    console.log("setIsRegistering", status);
    state.isRegistering = status; // 更新注册状态
  },
  setIsForgotPassword(state, status) {
    console.log("setIsForgotPassword", status);
    state.isForgotPassword = status; // 更新重置密码状态
  },
  setAccessToken(state, token: string) {
    // 添加设置 accessToken 的 mutation
    state.accessToken = token;
  },
  clearUser(state) {
    state.userInfo = null;
    state.isLoggedIn = false;
    state.isRegistering = false; // 登出时清空注册状态
  },
  // 新增 mutation 用于更新 formState
  updateFormState(state, payload: Partial<State["formState"]>) {
    state.formState = { ...state.formState, ...payload };
  },
  clearFormState(state) {
    // 清空 formState 的 mutation
    state.formState = {
      username: "",
      email: "",
      password: "",
      confirmPassword: "",
    };
  },
  clearResetPwdFormState(state) {
    // 清空 resetPwdFormState 的 mutation
    state.resetPwdFormState = {
      username: "",
      email: "",
    };
  },
};

// 定义 Actions
const actions: Actions = {
  login(
    { commit },
    { userData, token }: { userData: UserData; token: string }
  ) {
    console.log("触发actions::login");
    console.log("传入的 userData:", userData);
    console.log("传入的 token:", token);
    commit("setUser", userData);
    commit("setAccessToken", token); // 保存 accessToken账号注册
  },
  logout({ commit }) {
    commit("clearUser");
    localStorage.removeItem("accessToken"); // 清除本地存储中的 token
    stopRefreshTimer(); // 停止刷新 token 的定时器
  },
  registerUser({ commit }, payload: { userData: UserData }) {
    console.log("触发actions::registerUser");
    console.log("传入的 userData:", payload.userData);
    commit("setUser", payload.userData);
    // 处理注册逻辑后
    commit("setIsRegistering", false); // 注册完成后，重置注册状态
    commit("setIsLoggedIn", false); // 可选，视你的需求而定
    commit("clearFormState");
    // router.push("/login");
    // TODO: 注册用户
  },
  resetPassword({ commit }, payload: { userData: UserResetPwdData }) {
    console.log("触发actions::resetPassword");
    console.log("传入的 userData:", payload.userData);
    // 处理重置密码逻辑后
    commit("setIsForgotPassword", false); // 重置密码完成后，重置密码状态
    commit("clearFormState");
    commit("clearResetPwdFormState");
    // router.push("/login");
    // TODO: 重置密码
  },
  async fetchAccessToken({ commit }) {
    try {
      const token = await getAccessToken();
      if (token) {
        commit("setAccessToken", token);
      }
    } catch (error) {
      console.error("获取 accessToken 失败:", error);
    }
  },
};

// 定义 Getters
const getters: Getters = {
  getUserInfo(state) {
    return state.userInfo;
  },
  isLoggedIn(state) {
    return state.isLoggedIn;
  },
  getAccessToken(state) {
    // 添加 Getter 获取 accessToken
    return state.accessToken;
  },
  getFormState(state) {
    // Getter 获取 formState
    return state.formState;
  },
  getResetPwdFormState(state) {
    // Getter 获取重置密码的 formState
    return state.resetPwdFormState;
  },
};

// 创建 Vuex 模块
// eslint-disable-next-line
const userStore: Module<State, any> = {
  namespaced: true, // 在模块级别设置命名空间
  state,
  mutations,
  actions,
  getters,
};

export default userStore;
